<?php

namespace app\common\service;

use app\common\model\Banner as BannerModel;

class BannerService
{
    /**
     * 获取轮播图片
     * @param $page
     * @param $pageSize
     * @param $where
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getList($page, $pageSize, $where)
    {
        $start = ($page - 1) * $pageSize;
        $count = BannerModel::where($where)->count();
        $list = BannerModel::where($where)->order('id', 'desc')->limit($start, $pageSize)->select();
        return ['total' => $count, 'list' => $list];
    }

    /**
     * 新增轮播图
     * @param $data
     * @return BannerModel|\think\Model
     */
    public function create($data)
    {
        return BannerModel::create($data);
    }

    /**
     * 更新轮播图数据
     * @param $data
     * @param $id
     * @return BannerModel
     */
    public function update($data, $id)
    {
        return BannerModel::update($data, ['id' => $id]);
    }

    /**
     * 删除轮播图
     * @param $id
     */
    public function remove($id)
    {
        BannerModel::destroy($id);
    }

    /**
     * 获取指定轮播图信息
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getOne($id)
    {
        return BannerModel::where(['id'=>$id])->find();
    }
}